using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Diomede.SDK;

namespace WindowsTest
{
    public partial class UserInfoForm : Form
    {
        public UserInfoForm()
        {
            InitializeComponent();
        }

        private string userName;

        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }

        private string password;

        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        private string email;

        public string Email
        {
            get { return email; }
            set { email = value; }
        }


        private void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                ISession session = new Session();

                IAccountManagement accountManager = session as IAccountManagement;
                ISessionExecResult result = accountManager.Create(this.txtUsername.Text, this.txtPassword.Text, this.txtEmail.Text);
                if (result.Status != Diomede.SDK.Enums.ExecutionResults.Success)
                    throw new Exception(result.Message);

                result = session.Login(txtUsername.Text, txtPassword.Text);
                if (result.Status != Diomede.SDK.Enums.ExecutionResults.Success)
                    throw new Exception(result.Message);

                accountManager = session as IAccountManagement;
                com.diomede.services.data.UserInfo userInfo = new com.diomede.services.data.UserInfo();
                userInfo.cardNumber = txtCcNumber.Text;
                userInfo.cardName = txtCcName.Text;
                userInfo.cardAddress1 = txtCcAddress.Text;
                userInfo.cardCity = txtCcCity.Text;
                userInfo.cardState = txtCcState.Text;
                userInfo.cardCountry = txtCcCountry.Text;
                userInfo.cardZip = txtCcZip.Text;
                userInfo.cardExpiryYear = int.Parse(txtCcExpiryYear.Text);
                userInfo.cardExpiryMonth = int.Parse(txtCcExpiryMonth.Text);

                result = accountManager.UpdateAccountInfo(userInfo);
                if (result.Status != Diomede.SDK.Enums.ExecutionResults.Success)
                    throw new Exception(result.Message);

                session.Logout();
                
                userName = txtUsername.Text;
                password = txtPassword.Text;
                email = txtEmail.Text;

                MessageBox.Show(this, "Account Successfully created.");
                this.DialogResult = DialogResult.OK;
                this.Close();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}